﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
using MM.Translator.Configuration;
using MM.Translator.Model;

namespace MM.Translator
{
    public partial class MainPage
    {
        private LanguageItem _sourceLanguage;
        private LanguageItem _targetLanguage;
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            InitializeData();
        }

        private void InitializeData()
        {
            var languageList = LanguageService.Current.GetLanguageData();

            _sourceLanguage = new LanguageItem() {Code = AppSettings.Current.SourceLanguage};
            _targetLanguage = new LanguageItem() {Code = AppSettings.Current.TargetLanguage};
            
            listPickerSourceLanguage.ItemsSource = languageList;
            listPickerTargetLanguage.ItemsSource = languageList;            
            
            SetSelectedItems();

            listPickerSourceLanguage.SelectionChanged += OnSourceLanguageChanged;
            listPickerTargetLanguage.SelectionChanged += OnTargetLanguageChanged;
        }

        private void SetSelectedItems()
        {
            listPickerSourceLanguage.SelectedItem = _sourceLanguage;
            listPickerTargetLanguage.SelectedItem = _targetLanguage;
        }

        private void OnTargetLanguageChanged(object sender, SelectionChangedEventArgs e)
        {
            var selected = e.AddedItems[0] as LanguageItem;
            if (selected == _targetLanguage) 
                return;

            TargetLanguage = selected;
        }

        private void OnSourceLanguageChanged(object sender, SelectionChangedEventArgs e)
        {
            var selected = e.AddedItems[0] as LanguageItem;
            if (selected == _sourceLanguage)
                return;

            SourceLanguage = selected;
        }

        public LanguageItem SourceLanguage
        {
            get { return _sourceLanguage; }
            set
            {
                _sourceLanguage = value;
                if (_sourceLanguage != null)
                    AppSettings.Current.SourceLanguage = _sourceLanguage.Code;

            }
        }

        public LanguageItem TargetLanguage
        {
            get { return _targetLanguage; }
            set
            {
                _targetLanguage = value;
                if (_targetLanguage != null)
                    AppSettings.Current.TargetLanguage = _targetLanguage.Code;
            }
        }

        private void Translate(string inputText, string fromLanguage, string toLanguage)
        {
            TranslationService.Current.TranslationCompleted += OnTranslationCompleted;
            TranslationService.Current.Translate(inputText, fromLanguage, toLanguage);
        }

        private void OnTranslationCompleted(object sender, TranslationEventArgs e)
        {
            TextBoxResult.Text = e.Result;
        }

        private void OnTranslate(object sender, RoutedEventArgs e)
        {
            if (SourceLanguage == TargetLanguage)
                MessageBox.Show("You must select two different languages.");

            if (SourceLanguage != null && TargetLanguage != null) 
                Translate(TextBoxText.Text, SourceLanguage.Code, TargetLanguage.Code);
        }

        private void OnReverse(object sender, RoutedEventArgs e)
        {
            var language = SourceLanguage;

            SourceLanguage = TargetLanguage;
            TargetLanguage = language;

            SetSelectedItems();
        }

        private void OnApplicationSetting(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AboutPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}
